<template>
    <div class="tags-select" ref="modalRef">
        <div class="title-content">
            <div
                class="item-title"
                v-for="(item, index) in listData"
                :key="index"
                @click="onTitle(item.code)"
                :class="{ active: activeTitle === item.code }"
            >
                <span>{{ item.name }}</span>
                <i class="iconfont icon-down" />
            </div>
        </div>
        <div>{{ listData.name }}</div>
        <div
            class="select-content"
            :class="{ active: activeTitle === item.code }"
            v-for="(item, index) in listData"
            :key="index"
        >
            <div
                class="item0"
                v-for="(_item, _index) in item.children"
                :key="_index"
                v-show="_item.children.length"
            >
                <div class="left-label">{{ _item.name }} :</div>
                <div class="rr">
                    <div
                        class="item00"
                        @click="onSelectAll(_item.children)"
                        :class="{ has: !hasItemAll(_item.children) }"
                    >
                        全部
                    </div>
                    <div
                        class="item00"
                        v-for="(_item0, _index0) in _item.children"
                        :key="_index0"
                        @click="
                            onSelectItem(_item0.code, _item0.name, _item.code)
                        "
                        :class="{ has: hasItem(_item0.code, _item0.name) }"
                    >
                        <span>{{ _item0.name }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, computed } from "vue";
import { onClickOutside } from "@vueuse/core";

const props = defineProps({
    data: {
        type: Object,
        default: () => [],
    },
    select: {
        type: Object,
        default: () => [],
    },
});

const modalRef = ref(null);
const activeTitle = ref("");

onClickOutside(modalRef, () => {
    activeTitle.value = "";
});

//标签是否选中
const hasItem = (code, label) => {
    let result = false;
    if (props.select.includes(code)) {
        result = true;
    }
    return result;
};

//全部是否选中
const hasItemAll = (arr) => {
    let result = false;
    let _value = [];
    arr.map((item) => _value.push(item.code));
    props.select.map((item) => {
        if (_value.includes(item)) result = true;
    });
    return result;
};

//点击标题
const onTitle = (val) => {
    if (activeTitle.value === val) activeTitle.value = "";
    else activeTitle.value = val;
};

const emits = defineEmits(["selectTag", "selectTagll"]);

//点击标签
const onSelectItem = (code, name, pCode) => {
    emits("selectTag", pCode, code, name);
};

//点击全部
const onSelectAll = (arr) => {
    let _value = [],
        _label = [];
    arr.map((item) => {
        _value.push(item.code);
        _label.push(item.name);
    });
    emits("selectTagAll", _value, _label);
};

//listData
const listData = computed(() => {
    let allData = {
        code: "all-tags",
        name: "全部标签",
        children: [],
    };
    props.data.map((item) => {
        allData.children.push(...item.children);
    });
    let data0 = [allData, ...props.data];
    return data0;
});
</script>

<style lang="less" scoped>

.tags-select {
    position: relative;
    width: 100%;
}

.title-content {
    line-height: 24px;
    display: flex;
    align-items: center;
    .item-title {
        padding: 0 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        margin-right: 10px;
        transition: all 0.25s ease-in-out;
        box-sizing: border-box;
        border: 1px solid transparent;
        position: relative;
        &::after {
            content: "";
            position: absolute;
            bottom: -2px;
            left: 0;
            right: 0;
            height: 0;
            width: 100%;
            background: var(--bg-color-1);
            z-index: 103;
            transition: all 0.2s ease-in-out;
        }
        &.active {
            color: var(--primary-color);
            background: var(--bg-color-1);
            border-color: var(--border-color);
            border-bottom-color: var(--bg-color-1);
            border-top-left-radius: 2px;
            border-top-right-radius: 2px;
            svg {
                transform: rotate(180deg);
            }
            &::after {
                height: 2px;
            }
        }
        svg {
            transition: all 0.25s ease-in-out;
            width: 13px;
            color: #777;
            margin-left: 6px;
        }
        &:hover {
            color: var(--primary-color);
        }
    }
}
.select-content {
    position: absolute;
    left: -30px;
    right: 0;
    transform: scaleY(0);
    background-color: var(--bg-color-1);
    padding: 8px 20px 16px;
    border: 1px solid var(--border-color);
    transition: all 0.25s ease-in-out;
    transform-origin: center top;
    border-radius: 2px;
    z-index: 100;
    &.active {
        transform: scaleY(1);
        z-index: 102;
    }

    .item0 {
        display: flex;
        align-items: flex-start;
        line-height: 24px;
        margin-bottom: 6px;
        &:last-child {
            margin-bottom: 0;
        }
        .left-label {
            margin-top: 8px;
            margin-right: 12px;
            color: var(--text-color-base-2);
        }
        .rr {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            .item00 {
                margin-top: 8px;
                margin-right: 10px;
                cursor: pointer;
                padding: 0 8px;
                border-radius: 2px;
                transition: all 0.15s ease-in-out;
                &:hover {
                    background-color: var(--primary-color);
                    color: #fff;
                }
                &.has {
                    background-color: var(--primary-color);
                    color: #fff;
                }
            }
        }
    }
}
</style>
